package cn.xspace.wnx.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.PreferencesUtils;

import cn.xspace.library.base.MyBaseActivity;
import cn.xspace.library.ui.activity.LoginAct;
import cn.xspace.wnx.R;

public class RegisterAct2 extends MyBaseActivity {
    private View phoneIF;
    private EditText edPhone;
    private EditText edPass;
    private EditText edNewPass;
    private EditText edCode;
    private Button submit;
    private ImageView imgCode;
    private boolean[] ifboo = {false, false, false, false};
    private String codeKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registeract2);
        iniView();
    }

    private void iniView() {

        // NotEmptyTextWatcher listenerWatcher = new NotEmptyTextWatcher();
        phoneIF = findViewById(R.id.loading_if);

        edPhone = (EditText) findViewById(R.id.registeract_input_mobile_edtTxt);
        edPass = (EditText) findViewById(R.id.registeract_password_edtTxt);
        edNewPass = (EditText) findViewById(R.id.registeract_confirm_password_edtTxt);
        edCode = (EditText) findViewById(R.id.registeract_vercode_edtTxt);

//        CheckBox passIF = (CheckBox) findViewById(R.id.registeract_showpwd_chk);

        submit = (Button) findViewById(R.id.registeract_register_btn);

        imgCode = (ImageView) findViewById(R.id.registeract_vercode_img);
        imgCode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (ifboo[0]) {
                    imgCode.setImageResource(0);
                    ifCode();
                }
            }
        });

        edPhone.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edPhone.getText().toString().length() == 11) {
                    phoneIF.setVisibility(View.VISIBLE);
                    ifPhone();
                } else {
                    phoneIF.setVisibility(View.GONE);
                    Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_not_synced);
                    drawable.setBounds(0, 0, 60, 60);
                    edPhone.setError("您的手机号不足11位", drawable);
                    ifboo[0] = false;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        edPass.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edPass.getText().toString().length() >= 6) {
                    Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_synced);
                    drawable.setBounds(0, 0, 60, 60);
//                    if(ifboo[1]){
//                        edPass.setCompoundDrawables(null, null, drawable, null);
//                        return;
//                    }
                    edPass.setError("正确", drawable);
                    ifboo[1] = true;
                } else {
                    Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_not_synced);
                    drawable.setBounds(0, 0, 60, 60);
                    edPass.setError("您输入的密码不得小于6位", drawable);
                    ifboo[1] = false;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        edNewPass.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edNewPass.getText().toString().length() >= 6) {
                    if (edPass.getText().toString()
                            .equals(edNewPass.getText().toString())) {
                        Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_synced);
                        drawable.setBounds(0, 0, 60, 60);
//                        if(ifboo[2]){
//                            edNewPass.setCompoundDrawables(null, null, drawable, null);
//                            return;
//                        }
                        edNewPass.setError("正确", drawable);
                        ifboo[2] = true;
                    } else {
                        Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_not_synced);
                        drawable.setBounds(0, 0, 60, 60);
                        edNewPass.setError("您两次输入的密码不相同", drawable);
                        ifboo[2] = false;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        edCode.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edCode.getText().toString().length() == 6) {
                    if (edCode.getText().toString().equals(codeKey)) {
                        Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_synced);
                        drawable.setBounds(0, 0, 60, 60);
//                        if (ifboo[3]) {
//                            edCode.setCompoundDrawables(null, null, drawable, null);
//                            return;
//                        }
                        edCode.setError("正确", drawable);
                        ifboo[3] = true;
                        submit.setEnabled(true);
                    } else {
                        Drawable drawable = getResources().getDrawable(R.drawable.offline_notebook_not_synced);
                        drawable.setBounds(0, 0, 60, 60);
                        edCode.setError("您输入的验证码不正确", drawable);
                        ifboo[3] = false;
                        submit.setEnabled(false);
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }

    private void ifPhone() {
    }

    private void ifCode() {

    }

    private void ifSubmit() {
    }

    public void showpwd(View view) {
        CheckBox cb = (CheckBox) findViewById(R.id.registeract_showpwd_chk);
        if (cb.isChecked()) {
            edPass.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            edNewPass.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            edPass.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            edNewPass.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }

    public void register(View view) {
        ifSubmit();
    }

    public void back(View view) {
        finish();
        goLoginAct(null);
    }

    public void registerSuccess(String jid) {
        String mobile = edPhone.getText().toString().trim();
        PreferencesUtils.setPfValue(Constants.ACCOUNT_BAREJID_KEY, jid);
        PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_MOBILE, mobile);
        PreferencesUtils.setPfValue(Constants.ACCOUNT_PASSWORD_KEY, edPass.getText().toString().trim());
        goLoginAct(mobile);
    }

    /* 跳转登陆界面 */
    private void goLoginAct(String mobile) {
        Intent i = new Intent();
        i.setClass(this, LoginAct.class);
        if (mobile != null)
            i.putExtra("phone", mobile);
        startActivity(i);
        finish();
    }
}